/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.service;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ProtocolParameter;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-8-13 上午11:10:28          dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class HostConnectionService {
    private static final Logger logger = Logger.getLogger(HostConnectionService.class);
    private static final String HOSTNAME_CMDSTR = "hostname";

    public Boolean testConn(ProtocolParameter protocolParameter, String flag) {
        String ip = protocolParameter.getIp();

        List<String> list = new ArrayList<String>();
        BufferedReader reader = null;
        String strLine = null;
        try {
            if (flag.equals("1")) {
                //获取主机名
                reader = CommandReader.getReader(ip, HOSTNAME_CMDSTR, 0, 0, protocolParameter.getPort(), protocolParameter.getMaxConn(), protocolParameter.getProtocol(), protocolParameter.getUserName(), protocolParameter.getPwd(), protocolParameter.getPrompt());
            } else {
                //获取主机名
                reader = CommandReader.getReader(ip, HOSTNAME_CMDSTR, 0, 0);
            }
            if (reader != null) {
                while ((strLine = reader.readLine()) != null) {
                    list.add(strLine.trim());
                }
                reader.close();
            }
        } catch (Exception e) {
            logger.error("failed to get hostname ", e);
        }

        if (list != null && list.size() != 0) {
            return true;
        } else {
            return false;
        }

    }
}
